package com.xhtt.modules.app.controller.reportform;

import com.xhtt.common.exception.RRException;
import com.xhtt.common.exception.RRExceptionEnum;
import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.ExportExcel;
import com.xhtt.common.utils.R;
import com.xhtt.modules.reportform.model.DiscussCompanyTime;
import com.xhtt.modules.reportform.model.UserTaskCount;
import com.xhtt.modules.reportform.service.KpiReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 统计分析-KPI统计报表
 *
 * @Date 2019/3/15 14:32
 **/
@RestController
@RequestMapping(value = "app/report/kpi")
@Api(value = "统计分析-KPI统计报表", tags = "统计分析-KPI统计报表")
public class KPIReportController {

    @Autowired
    private KpiReportService kpiReportService;

    @GetMapping(value = "/team")
    @ApiOperation("班组工作数量统计")
    public R team(@RequestParam Map<String, Object> params) {
        return R.ok().put(Constant.DATA, kpiReportService.team(params));
    }

    @GetMapping(value = "/team/export")
    @ApiOperation("班组工作数量统计-导出")
    public void teamExport(HttpServletResponse response, @RequestParam Map<String, Object> params) {
        List<Map<String, Object>> result = kpiReportService.team(params);
        ExportExcel exportExcel = new ExportExcel();
        params.put("limit", "650000");
        String fileName = "班组工作数量统计";
        String sheetName = "sheet1";
        String[] headers = {"班组", "已完成", "未完成"};
        String[] columns = {"name", "count1", "count2"};

        try {
            exportExcel.exportExcel(fileName, sheetName, headers, columns, result, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException(RRExceptionEnum.EXPORT_FAIL);
        }
    }

    @GetMapping(value = "/time")
    @ApiOperation("同任务时间分类统计")
    public R time(@RequestParam Map<String, Object> params) {
        List<Map<String, Object>> result = kpiReportService.time(params);
        //处理
        Set<String> planNames = result.stream().map(m -> (String) m.get("plan_name")).collect(Collectors.toSet());//所有任务名
        Set<String> teamNames = result.stream().map(m -> (String) m.get("team_name")).collect(Collectors.toSet());//所有班组
        //构造一个字典
        Map<String, DiscussCompanyTime> map = new HashMap<>();
        result.forEach(m -> {
            String key = m.get("plan_name") + "$" + m.get("team_name");
            Integer time = null == m.get("time") ? 0 : Integer.valueOf(m.get("time").toString());
            String showTime = null == m.get("show_time") ? "0时0分" : (String) m.get("show_time");
            DiscussCompanyTime dct = new DiscussCompanyTime("", time, showTime);
            map.put(key, dct);
        });

        List<List<DiscussCompanyTime>> parent = new ArrayList<>();

        teamNames.forEach(teamName -> {
            List<DiscussCompanyTime> child = new ArrayList<>();
            planNames.forEach(planName -> child.add(map.get(planName + "$" + teamName)));
            parent.add(child);
        });
        return R.ok().put(Constant.DATA, result).put("planNames", planNames).put("teamNames", teamNames).put("parent", parent);
    }

    @GetMapping(value = "/time/export")
    @ApiOperation("同任务时间分类统计-导出")
    public void timeExport(HttpServletResponse response, @RequestParam Map<String, Object> params) {
        List<Map<String, Object>> result = kpiReportService.time(params);
        ExportExcel exportExcel = new ExportExcel();
        params.put("limit", "650000");
        String fileName = "同任务时间分类统计";
        String sheetName = "sheet1";
        String[] headers = {"任务", "班组", "任务时间"};
        String[] columns = {"plan_name", "team_name", "show_time"};

        try {
            exportExcel.exportExcel(fileName, sheetName, headers, columns, result, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException(RRExceptionEnum.EXPORT_FAIL);
        }
    }

    @GetMapping(value = "/user")
    @ApiOperation("不同员工任务数统计")
    public R user(@RequestParam Map<String, Object> params) {
        return R.ok().put(Constant.DATA, kpiReportService.user(params));
    }

    @GetMapping(value = "/user/export")
    @ApiOperation("不同员工任务数统计-导出")
    public void userExport(HttpServletResponse response, @RequestParam Map<String, Object> params) {
        List<UserTaskCount> result = kpiReportService.user(params);
        ExportExcel exportExcel = new ExportExcel();
        params.put("limit", "650000");
        String fileName = "不同员工任务数统计";
        String sheetName = "sheet1";
        String[] headers = {"姓名", "任务数量", "担任主要负责人数量"};
        String[] columns = {"name", "allCount", "mainCount"};

        try {
            exportExcel.exportExcel(fileName, sheetName, headers, columns, result, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException(RRExceptionEnum.EXPORT_FAIL);
        }
    }

    @GetMapping(value = "/carUser")
    @ApiOperation("装车人员KPI统计")
    public R carUser(@RequestParam Map<String, Object> params) {
        return R.ok().put(Constant.DATA, kpiReportService.carUser(params));
    }

    @GetMapping(value = "/carUser/export")
    @ApiOperation("装车人员KPI统计")
    public void carUserExport(HttpServletResponse response, @RequestParam Map<String, Object> params) {
        List<Map<String, Object>> result = kpiReportService.carUser(params);
        ExportExcel exportExcel = new ExportExcel();
        params.put("limit", "650000");
        String fileName = "装车人员KPI统计";
        String sheetName = "sheet1";
        String[] headers = {"姓名", "装车数量", "装车时间", "装车效率"};
        String[] columns = {"name", "count", "show_time", "show_time_avg"};

        try {
            exportExcel.exportExcel(fileName, sheetName, headers, columns, result, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException(RRExceptionEnum.EXPORT_FAIL);
        }
    }

}
